﻿using System;
using System.Diagnostics.Contracts;
using System.Threading;

namespace MathService
{
    public class ObjectAnimator : Processor
    {
        private readonly Action<int, int> f_AnimationAction;
        private readonly int f_SamplesCount;
        private int f_Sample;

        public int Timeout { get { return JoinThreadTimeout; } set { JoinThreadTimeout = value; } }

        public ObjectAnimator(Action<int, int> Animator, int SamplesCount, int Timeout)
        {
            Contract.Requires(Animator != null);
            Contract.Requires(SamplesCount > 0);
            Contract.Requires(Timeout >= 0);

            f_AnimationAction = Animator;
            f_SamplesCount = SamplesCount;
            this.Timeout = Timeout;
        }

        /// <summary>Основной метод действия процессора, вызываемое в цикле. Должно быть переопределено в классах-наследниках</summary>
        protected override void MainAction()
        {
            f_AnimationAction(f_Sample, f_SamplesCount);
            f_Enabled = f_Sample++ < f_SamplesCount;
            Thread.Sleep(Timeout);
        }
    }
}
